package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese05Paypolvers;
import ar.com.ford.it.fullEconomics.struct.FullEconomicWrapStruct;
import ar.com.ford.it.fullEconomics.struct.PaymentPolicyDisplayStruct;

public class PaymentPolicyForm extends ValidatorForm {

    private String section;
    private String supplierClass;
    private String partClass;
    private String supplierCode;
    private String commodity;
    private String prefix;
    private String base;
    private String suffix;
    private String itemCategory;
    private String item;
    private String aprovedOnesOnly;
    private Collection<PaymentPolicyDisplayStruct> payPolVersCol;
    private Teese05Paypolvers payPolVersSelected;
    private FullEconomicWrapStruct fullEconomic;

    public void resetForm() {
        this.section = null;
        this.supplierClass = "-1";
        this.partClass = "F";
        this.supplierCode = null;
        this.commodity = null;
        this.prefix = null;
        this.base = null;
        this.suffix = null;
        this.itemCategory = null;
        this.item = null;
        this.aprovedOnesOnly = "false";
        this.payPolVersCol = null;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ("editVersion".equalsIgnoreCase(this.getSection())
                || "newVersion".equalsIgnoreCase(this.getSection())
                || "delete".equalsIgnoreCase(this.getSection())) {

            if (this.getPayPolVersSelected() == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "paymentPolicy.error.invalidSelection"));
            }
        }
        if (ae.size() == 0) {
            if ("delete".equalsIgnoreCase(this.getSection())) {
                if (this.getPayPolVersSelected().getEese05PpyverApprovalUserC() != null) {
                    if (this.getPayPolVersSelected().getEese05PpyverOutDateY()
                            .getTime() > System.currentTimeMillis()) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "paymentPolicy.error.notDeletable"));
                    }
                }
            }
        }
        return ae;
    }

    /**
     * Setea la politica de pago seleccionada para luego ser modificada
     * 
     * @param key
     * @param value
     */
    public void setPayPolMapped(String key, String value) {
        List payPolVersCol = (List) this.getPayPolVersCol();
        PaymentPolicyDisplayStruct rowSelected = (PaymentPolicyDisplayStruct) payPolVersCol
                .get(Integer.parseInt(key) - 1);

        this.setPayPolVersSelected(rowSelected.getDisplayVersion());
    }

    public String getSelectedPpIndex() {
        return "false";
    }

    /**
     * Setea la politica de pago seleccionada para luego ser modificada
     * 
     * @param key
     * @param value
     */
    public void setSelectedPpIndex(String selectedIndex) {
        List payPolVersCol = (List) this.getPayPolVersCol();
        PaymentPolicyDisplayStruct rowSelected = (PaymentPolicyDisplayStruct) payPolVersCol
                .get(Integer.parseInt(selectedIndex) - 1);

        this.setPayPolVersSelected(rowSelected.getDisplayVersion());
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base.toUpperCase();
    }

    public String getCommodity() {
        return commodity;
    }

    public void setCommodity(String commodity) {
        this.commodity = commodity;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getItemCategory() {
        return itemCategory;
    }

    public void setItemCategory(String itemCategory) {
        this.itemCategory = itemCategory;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix.toUpperCase();
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix.toUpperCase();
    }

    public String getSupplierClass() {
        return supplierClass;
    }

    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode.toUpperCase();
    }

    public String getAprovedOnesOnly() {
        return aprovedOnesOnly;
    }

    public void setAprovedOnesOnly(String aprovedOnesOnly) {
        this.aprovedOnesOnly = aprovedOnesOnly;
    }

    public String getPartClass() {
        return partClass;
    }

    public void setPartClass(String partClass) {
        this.partClass = partClass;
    }

    public Collection<PaymentPolicyDisplayStruct> getPayPolVersCol() {
        return payPolVersCol;
    }

    public void setPayPolVersCol(
                                 Collection<PaymentPolicyDisplayStruct> payPolVers) {
        this.payPolVersCol = payPolVers;
    }

    public Teese05Paypolvers getPayPolVersSelected() {
        return payPolVersSelected;
    }

    public void setPayPolVersSelected(Teese05Paypolvers payPolVersSelected) {
        this.payPolVersSelected = payPolVersSelected;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.aprovedOnesOnly = "false";

    }

    public FullEconomicWrapStruct getFullEconomic() {
        return fullEconomic;
    }

    public void setFullEconomic(FullEconomicWrapStruct fullEconomic) {
        this.fullEconomic = fullEconomic;
    }

}
